﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Web;
using FZ.Common;
using TongJi.Bll;
using TongJi.Commmon;
using TongJi.Model;

namespace TongJi.Web
{
    /// <summary>
    /// Log 的摘要说明
    /// </summary>
    public class Log : IHttpAsyncHandler
    {

        private Action<HttpContext> action = (HttpContext context) =>
        {
            var manager = new TongJiManager();
            string msg;
            var checkStatus = manager.CheckSiteStatus(context, out msg);
            if (checkStatus == CheckSiteStatusEnum.Success)
            {
                manager.InsertRequestLog(context);
            }
            else
            {
                context.Response.AddHeader(Consts.LogResponseHeaderKey, msg);
                // context.Response.Write("/*{0}*/".FormatWith(msg));
            }
            context.Response.ContentType = "image/jpg";
        };

        public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
        {
            return action.BeginInvoke(context, cb, extraData);
        }

        public void EndProcessRequest(IAsyncResult result)
        {
        }

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {

        }
    }
}